package com.xkstream.client.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.test.mock.MockApplication;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.xkstream.client.R;
import com.xkstream.client.utils.DeviceUtils;

/**
 * Created by xiaoyi on 2017-9-27.
 */

public class GainsProgressbar extends View {
    private double maxValue = 1000;
    private double currentValue = 800;

    private int currentWidth;

    private int background = Color.WHITE;
    private int progressColor = Color.parseColor("#cccccc");

    private int padding;

    private Paint mPaint;
    private int textSize;
    private int textColor;

    private String textTime = "2017-09-08";
    private String textProgress = "800";

    public void setTime(String textTime) {
        this.textTime = textTime;
    }

    public void setProgress(int progress) {
        this.textProgress = String.valueOf(progress);
        this.currentValue = progress;
    }

    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
    }

    public void setProgressColor(int color){
        this.progressColor = color;
    }


    public GainsProgressbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public GainsProgressbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setColor(background);
        padding = DeviceUtils.dp2px(getContext(), 8);

        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.GainsProgressbar);
        background = array.getColor(R.styleable.GainsProgressbar_barBackground, Color.WHITE);
        progressColor = array.getColor(R.styleable.GainsProgressbar_progressColor, progressColor);

        textSize = array.getDimensionPixelOffset(R.styleable.GainsProgressbar_barTextSize, DeviceUtils.sp2px(getContext(), 14));
        textColor = array.getColor(R.styleable.GainsProgressbar_barTextColor, Color.BLACK);

        array.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);

        mPaint.setColor(progressColor);

        drawProgress(canvas);

        drawTime(canvas);


    }

    private void drawProgress(Canvas canvas) {
        currentWidth = (int) (getWidth() * currentValue / maxValue + 0.5);
        canvas.drawRect(0, 0, currentWidth, getHeight(), mPaint);
    }

    private void drawTime(Canvas canvas) {
        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);
        mPaint.setTextAlign(Paint.Align.LEFT);
        Rect bounds = new Rect();
        mPaint.getTextBounds(textTime, 0, textTime.length(), bounds);
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(textTime, padding, baseline, mPaint);
        drawProgressText(canvas);
        mPaint.setColor(Color.WHITE);
    }

    private void drawProgressText(Canvas canvas) {
        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);
        mPaint.setTextAlign(Paint.Align.LEFT);
        Rect bounds = new Rect();
        mPaint.getTextBounds(textProgress, 0, textProgress.length(), bounds);
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(textProgress, currentWidth - bounds.width() - padding, baseline, mPaint);
    }
}
